package io.renren.modules.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.web.dao.CategoryDao;
import io.renren.modules.web.entity.CategoryEntity;
import io.renren.modules.web.service.CategoryService;
import io.renren.modules.web.vo.CategoryLayerDetailVo;
import io.renren.modules.web.vo.CategoryLayerVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> getCategoryList() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        return categoryEntities.stream()
                .filter(categoryEntity -> categoryEntity.getParentId() == 0)
                .peek(parent -> {
                    // 找到了一级菜单，递归获取子菜单
                    List<CategoryEntity> children = getChildren(categoryEntities, parent);
                    parent.setChildren(children);
                })
                .sorted(Comparator.comparingInt(CategoryEntity::getPriority))
                .collect(Collectors.toList());
    }

    @Override
    public CategoryLayerVo getCategoryLayer(Long id) {
        ArrayList<Long> idList = new ArrayList<>();
        ArrayList<String> nameList = new ArrayList<>();
        CategoryEntity categoryEntity = getById(id);
        String categoryEntityName = categoryEntity.getName();
        Long categoryEntityId = categoryEntity.getId();
        idList.add(categoryEntityId);
        nameList.add(categoryEntityName);
        getParent(categoryEntity, idList, nameList);
        CategoryLayerVo categoryLayerVo = new CategoryLayerVo();
        Collections.reverse(idList);
        Collections.reverse(nameList);
        categoryLayerVo.setCategoryId(id);
        categoryLayerVo.setCategoryIdLayer(idList);
        categoryLayerVo.setCategoryNameLayer(nameList);
        return categoryLayerVo;
    }

    private void getParent(CategoryEntity child, List<Long> ids, List<String> names) {
        if (child.getParentId() != 0) {
            CategoryEntity categoryEntity = getById(child.getParentId());
            ids.add(categoryEntity.getId());
            names.add(categoryEntity.getName());
            getParent(categoryEntity, ids, names);
        }
    }

    private List<CategoryEntity> getChildren(List<CategoryEntity> list, CategoryEntity parent) {
        return list.stream()
                .filter(categoryEntity -> categoryEntity.getParentId() != 0)
                .filter(categoryEntity -> categoryEntity.getParentId().equals(parent.getId()))
                .peek(categoryEntity -> {
                    List<CategoryEntity> children = getChildren(list, categoryEntity);
                    categoryEntity.setChildren(children);
                })
                .sorted(Comparator.comparingInt(CategoryEntity::getPriority))
                .collect(Collectors.toList());
    }
}